package com.shuai.controller.feign;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shuai.model.entity.UserInterfaceInfo;
import com.shuai.common.ErrorCode;
import com.shuai.exception.BusinessException;
import com.shuai.service.UserInterfaceInfoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @ClassName InnerUserInterfaceInfoServiceImpl
 * @Description TODO
 * @Author OTTO
 * @Date 2023/1/10 20:25
 */
@RestController
@RequestMapping("/feign")
public class InnerUserInterfaceInfoController {


    @Resource
    private UserInterfaceInfoService userInterfaceInfoService;

    /**
     * 接口调用次数减一
     * @param interfaceInfoId
     * @param userId
     * @return
     */
    @GetMapping("/invokeCount")
    public boolean invokeCount(@RequestParam("interfaceInfoId") long interfaceInfoId, @RequestParam("userId") long userId) {
        return userInterfaceInfoService.invokeCount(interfaceInfoId, userId);
    }

    /**
     * 判断剩余调用次数
     *
     * @param userId
     * @param interfaceInfoId
     * @return
     */
    @GetMapping("/validLeftNum")
    public boolean validLeftNum(@RequestParam("userId")Long userId,@RequestParam("interfaceInfoId") Long interfaceInfoId) {
        LambdaQueryWrapper<UserInterfaceInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserInterfaceInfo::getUserId, userId)
                .eq(UserInterfaceInfo::getInterfaceInfoId, interfaceInfoId);
        UserInterfaceInfo userInterfaceInfo = userInterfaceInfoService.getOne(lqw);
        if (userInterfaceInfo == null || userInterfaceInfo.getLeftNum() <= 0) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"剩余调用次数不足");
        }
        return true;
    }
}
